package com.csxy.six.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.six.entity.Goods;
import com.csxy.six.mapper.GoodsMapper;
import com.csxy.six.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    // 根据分类获取商品
    @Override
    public List<Goods> getGoodsByCategory(Integer categoryId) {
        return goodsMapper.selectList(new QueryWrapper<Goods>().eq("goods_cat_id", categoryId));
    }

    // 搜索商品
    @Override
    public List<Goods> searchGoods(String keyword) {
        return goodsMapper.selectList(new QueryWrapper<Goods>()
                .like("title", keyword)
                .or()
                .like("description", keyword));
    }

    // 更新商品状态
    @Override
    public boolean updateGoodsStatus(Integer goodsId, String newStatus) {
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods != null) {
            goods.setStatus(newStatus);
            return goodsMapper.updateById(goods) > 0;
        }
        return false;
    }

    // 增加商品浏览量
    @Override
    public boolean incrementViewCount(Integer goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods != null) {
            goods.setViewCount(goods.getViewCount() + 1);
            return goodsMapper.updateById(goods) > 0;
        }
        return false;
    }
}